<?php
Class tiviController Extends baseController {
	public function index() 
	{
			
	}
	
	public function detail() 
	{
		if(!isset($_REQUEST['tivi_id']) || !is_numeric($_REQUEST['tivi_id']))
		{
			header('location:'.__URL_BASE.'trang-chu.html');	
			exit;
		}
		$tivi_id = $_REQUEST['tivi_id'];
		$tivi = new tivi();
		$Tv = $tivi->getTiviById($tivi_id);
		
		$comment = new comment();
		$lstComment = $comment->getCommentByWhere('trangthai=1 and parent_id=0 and tivi_id = '.$tivi_id,'thoigian desc');
		
		$this->registry->template->Tivi = $Tv;
		$this->registry->template->lstComment = $lstComment;
		$this->registry->template->show('tivi_detail');	
	}

	public function category() { 
		$tivi = new tivi(); //echo '<pre>'; var_dump($hangsanxuat->getAllHangSanXuat()); die();
		$hangsanxuat = new hangsanxuat();
		$current_page = (isset($_REQUEST['page'])) ? $_REQUEST['page'] : 1;
		$pos = ($current_page -1 ) * __PAGE_SIZE;
		
		$lstTv = $tivi->getTiviByWherePaging('t.TrangThai = 1 and t.SoLuong > 0 and idHangSanXuat = ' .$_REQUEST['cate_id'],'',$pos);
		$count = count((array)$tivi->getTiviByWhere('t.TrangThai = 1 and t.SoLuong > 0 and idHangSanXuat = ' .$_REQUEST['cate_id'],''));
		$pages = ceil($count/__PAGE_SIZE);
		
		$this->registry->template->lstTivi = $lstTv;
		$this->registry->template->pages = $pages;
		$this->registry->template->show('index');
	}
	
	//hàm đẩy các điều kiện tìm kiếm lên url (đã rewrite)
	public function urlsearchtivi(){
		if(!isset($_POST['keyword']))
		{
			$gia =  $_POST['gia']  ;
			$hang =  $_POST['hang'] ;
			$loai =  $_POST['loai'] ;
			$kichthuoc = $_POST['kichthuoc'] ;
			//chuyển sang hàm searchTivi kèm theo các biến truyền lên url
			header('location:'.__URL_BASE.'tivi/tim-kiem-san-pham-'.$hang.'-'.$gia.'-'.$loai.'-'.$kichthuoc.'.html');
			exit;
		}
		else
		{
			$keyword = $_POST['keyword']; //echo $keyword; die;
			header('location:'.__URL_BASE.'tivi/tim-kiem-san-pham.html/keyword='.$keyword);
			exit;
		}
		
		
	}
	
	public function searchTivi(){
		$tivi = new tivi();
		if(!isset($_GET['keyword']))
		{
			
			$hang = $_GET['hang'] ? $_GET['hang'] : '0';
			$gia = $_GET['gia'] ? $_GET['gia'] : '0';
			$loai = $_GET['loai'] ? $_GET['loai'] : '0';
			$kichthuoc = $_GET['kichthuoc'] ? $_GET['kichthuoc'] : '0';
			
			//lấy ra điều kiên lọc
			$where = " t.SoLuong > 0 ";
			$where .= $loai == '0' ? '' : ' and l.id = '.$loai;
			$where .= $hang == '0' ? '' : ' and h.id = '.$hang;
			$where .= $kichthuoc == '0' ? '' : ' and t.KichThuoc = '.$kichthuoc;
			switch($gia){
				case '0': $where .= ''  ;
					break;
				case '1': $where .= ' and GiaBan < 7000000';
					break;
				case '2': $where .= ' and GiaBan >= 7000000 and GiaBan <= 15000000';
					break;
				case '3': $where .= ' and GiaBan >= 15000000 and GiaBan <= 30000000';
					break;
				case '4': $where .= ' and GiaBan > 30000000';
					break;
			}
			
			
		}
		else
		{
			$keyword = $_REQUEST['keyword']; 
			$where = "TenTivi like '%".$keyword."%'";
			
		}
		$where .= ' and h.TrangThai = 1 and t.TrangThai = 1 ';
			$current_page = (isset($_REQUEST['page'])) ? $_REQUEST['page'] : 1; //echo $_REQUEST['page'];die;
			$pos = ($current_page -1 ) * __PAGE_SIZE;
			$lstTv = $tivi->searchTivi($where,$pos);
			$count = count((array)$tivi->getTiviByWhere($where,''));
			$pages = ceil($count/__PAGE_SIZE);
			
			$this->registry->template->lstTivi = $lstTv;
			$this->registry->template->pages = $pages;
			$this->registry->template->show('index');
	}
	
	public function tiviSearchAjax(){
		$keyword = $_REQUEST['keyword']; 
		$tiviModel = new tivi();
		$lstTivi = $tiviModel->getTiviByWhere("TenTivi like '%".$keyword."%' and t.TrangThai = 1 and t.SoLuong > 0","");
		foreach($lstTivi as $item){
			$item->Images = __URL_BASE.'images/'.$item->Images;
			$link = utility::utf8tourl(trim($item->TenTivi));
			$item->Images2 = __URL_BASE.'tivi/chi-tiet-san-pham/'. $item->id.'/'.$link. 'html';
			$item->GiaBan = number_format($item->GiaBan,0,'','.');
		}
		$rs= json_encode($lstTivi); echo $rs; die; 
		//$arr['TinhNang1'] = 'Công nghệ quét hình MotionFlow XR 200Hz';
//		$arr['TinhNang2'] = 'Công nghệ loa Bass Reflex Speaker';
//		$arr['TinhNang3'] = 'Bộ xử lý hình ảnh X-Reality PRO';
//		$arr['TinhNang4'] = 'Internet TV - 3G và  Wifi tích hợp';
//		$arr['TinhNang5'] = 'Âm thanh Dolby Digital';
//		$arr['TinhNang6'] = 'Sony Entertainment Network với nhiều nội dung giải trí';
//		$arr['TinhNang7'] = 'Tiết kiệm điện,thân thiện MT';
//		
//		echo json_encode($arr);
	}
	
	
}
?>